#ifndef __WHOLE_MOTOR_PARAMETERS_H_
#define __WHOLE_MOTOR_PARAMETERS_H_
#include "HAL_device.h"
#include "HAL_conf.h"

#define ENABLE_DAC_SHOW // for enable DAC output
#define ENABLE_LED_SHOW // if disable, it can remove LED pin, key input pin I/O initialize

//------MCU Selection-------------------------------------------------------------------------------------------------------
#define ENABLE_SPIN2X

//------System parameters---------------------------------------------------------------------------------------------------
#define SYSYTEM_CLOCK 96000000 // unit:Hz
#define PWM_FREQUENCY 24000    // unit:HZ
#define POLE_NUMBER 8          // 8
#define MOTOR_DIR_CW 1         // 1:CW, 0:CCW rotation direction define

//----Enable/Disable 1 shunt R mode---------------------------------------------------------------------------------------------------------------
//#define ENABLE_1_SHUNT_R_TO_MEASURE_3_PHASE_CURRENT //if not define, it will use 2 shunt R to measure 3 phase current
#define ENABLE_LEFT_HALF_PWM_GET_IaIbIc_1_SHUNT_R // if defined, use left half pwm. if not define, use right half pwm sample phase currents

//------2 shunt R phase current sensing parameter-------------------------------------------------------------------------------------------------
#define MAX_PWM_DUTY_PERCENTAGE_2SHUNT_R 92 // unit:%, it need to limit the maximum pwm duty output for doing the 2 shunt R current sensing

//------1 shunt R phase current sensing parameter-------------------------------------------------------------------------------------------------
#define ADC_DELAY_TIME_1_SHUNT_R 30 // 25//35 //unit : 0.1us (dead time + OP output signal stable time)
#define ADC_TOTAL_TIME_1_SHUNT_R 45 // 42//37//47 //unit : 0.1us (dead time + OP output signal stable time + ADC sample time)

#ifdef ENABLE_SPIN2X
//#define ADC_TRIG_USE_TIM3_CC1_CC4 // 20201015
#define ADC_TRIG_USE_TIM1_CC4_CC5 // 20201015
#endif

//------MAX/MIN SPEED Limitation setup------------------------------------------------------------------------------------------------------------
#ifdef ENABLE_POWER_CMD_INPUT_CONTROL_TO_REPLACE_SPEED_CMD_INPUT_CONTROL // if enable, use power control to replace speed control
#define TARGET_MAX_SPEED (TARGET_SPEED_7 + (TARGET_SPEED_7 / 2))         // unit:RPM, max. speed limitation
#elif defined ENABLE_Iq_CMD_INPUT_CONTROL_TO_REPLACE_SPEED_CMD_INPUT_CONTROL
#define TARGET_MAX_SPEED (TARGET_SPEED_7 + (TARGET_SPEED_7 / 2)) // unit:RPM, max. speed limitation
#else
#define TARGET_MAX_SPEED TARGET_SPEED_7 // unit:RPM, max. speed limitation
#endif

#define TARGET_MIN_SPEED TARGET_SPEED_1 // unit:RPM, min. speed limitation
//************************************************************************************************************************************************************
//-----IPD1,2,3 (initial position detection) mode parameters-------------------------------------------------------------------------------------
//------Rotor initial position detection method 1 parameters-------------------------------------------------------------------------------------

#define ENABLE_ROTOR_IPD1_FUNCTION      // if define, it will use IPD1 function to detect the initial position of rotor before startup motor
#define ROTOR_IPD1_TARGET_CURRENT 30    // unit:0.1A define the target torque current in ROTOR_SMALL_SWING_MODE
#define ROTOR_IPD1_SMALL_SWING_TIME 100 // unit:ms, define rotor small swing time with ROTOR_IPD1_TARGET_CURRENT torque current
#define ROTOR_IPD1_DETECT_TIME 60       // unit:ms, define rotor angle detection time after rotor small swing, range 40~100ms
#define ROTOR_IPD1_BEMF_SUM_MINI 50     // unit:mV, define the minimum sum value of bemf |A| + |B| absolute value
#define ROTOR_IPD1_MAX_FAIL_TIMES 15    // range:(6~65535),if IPD failure times over this setup value, motor operation will enter open loop mode directly.

//------Rotor initial position detection method 2 parameters---inductance saturation---------------------------------------------------------------
//#define ENABLE_ROTOR_IPD2_FUNCTION            //if define, it will use IPD2 function to detect the initial position of rotor before startup motor

//---for 8cm big server fan
#define PWM_NUM_FOR_INJECT_VOLT 8      // define PWM numbers as IPD2's inject voltage pulse time, for motor inductance saturation effect //20190626
#define PWM_NUM_FOR_NON_INJECT_VOLT 24 // define PWM numbers as IPD2's off current time after inject voltage pulse to motor//20190626
#define INJECT_VOLT_PULSE_AMPLITUDE 10 // unit:%,  define motor inject voltage pulse amplitude, range (1~40)
#define PHASE_LEAD_ANGLE_FOR_IPD2 85   // 1024 = 360degree, so 85 = 30degree, phase lead angle setup

//------Rotor initial position detection method 3 parameters---high frequency injection---------------------------------------------------------------
//#define ENABLE_ROTOR_IPD3_FUNCTION                //if define, it will use IPD3 function to detect the initial position of rotor before startup motor

//---for 8cm big server fan
//---HFJ angle detection parameters setup
#define HFJ_FREQUENCY 1000       // unit:Hz,define the "high frequency injection" frequency
#define HFJ_IPD3_DETECT_TIME 600 // unit:1ms, define the HFJ angle detect time
#define HFJ_Vd_VOLT_AMPLITUDE 10 // unit:%, define motor inject voltage pulse amplitude, range (1~20)
#define INDUCTANCE_GAIN_K 30     // unit:0.1, define the Lq/(Lq-Ld) value
#define RESOLUTION_SHIFT 3       // unit: none, range(2~6), 2 slower HFJ angle estimate, 6 is faster HFJ angle estimate

//---HFJ polarity check parameters setup
#define HFJ_IPD3_SUSPEND_TIME 50           // unit:1ms, define the HFJ suspend time before doing 180degree polarity check//20191001
#define HFJ_PWM_NUM_FOR_INJECT_VOLT 8      // define PWM numbers as IPD3's inject voltage pulse time, for motor inductance saturation effect //20191001
#define HFJ_PWM_NUM_FOR_NON_INJECT_VOLT 24 // define PWM numbers as IPD3's off current time after inject voltage pulse to motor//20191001
#define HFJ_INJECT_VOLT_PULSE_AMPLITUDE 10 // unit:%,  define motor inject voltage pulse amplitude, range (1~40)
#define PHASE_LEAD_ANGLE_FOR_IPD3 85       // 1024 = 360degree, so 85 = 30degree, phase lead angle setup

//************************************************************************************************************************************************************
//-----New Startup mode parameters----------------------------------------------------------------------------------------------------------------------------
//-----define enable the new startup method and others parameters----------------------------------------------------------------------------------------------

//#define ENABLE_NEW_STARTUP_METHOD

//---for 24v nidec fan
#define NEW_STARTUP_MIN_TIME 60                                                 // unit:10ms, define new startup mode minimum time, 20 means 200ms as new startup time//20190626
#define NEW_STARTUP_MAX_TIME 300                                                // unit:100ms,if over this time in new startup mode, it will show error to system,600 means 60second//20190626
#define NEW_STARTUP_TO_CLOSE_LOOP_SPEED (TARGET_SPEED_1 - (TARGET_SPEED_1 / 3)) // unit:RPM, define enter close loop mode speed
#define NEW_STARTUP_RAMP_UP_TIME 90                                             // range(80~3200),16 = 1ms at 16KHz PWM, 1440 pwm cycles of 16khz = 1440/16 =90ms

#define NEW_STARTUP_INITIAL_CURRENT_CW 7  // unit:0.1A define the initial torque current in new startup mode for standstill or downwind startup //20200602
#define NEW_STARTUP_INITIAL_CURRENT_CCW 7 // unit:0.1A define the initial torque current in new startup mode for standstill or downwind startup //20200602

#define NEW_STARTUP_TARGET_CURRENT_CW 20  // unit:0.1A define the target  torque current in new startup mode for CW //20200602
#define NEW_STARTUP_TARGET_CURRENT_CCW 20 // unit:0.1A define the target  torque current in new startup mode for CCW//20200602

#define NEW_STARTUP_CURRENT_FOR_AGAINST_WIND 30 // unit:0.1A define the initial torque current in new startup mode for against wind startup //20190626
#define NEW_STARTUP_RAMP_UP_CURRENT_SLOP 30     // unit:0.1A/sec,range(1~400),increase how many torque current per second in new startup mode//20190626

#define NEW_STARTUP_RAMP_UP_ANGLE_CW 90  // 171 means 360*(171/1024) = 60degree//20200325
#define NEW_STARTUP_RAMP_UP_ANGLE_CCW 90 // 171 means 360*(171/1024) = 60degree//20200325

#define NEW_STARTUP_TORQUE_VOLT_LIMIT 10000        // range(1000~24000), for new startup mode torqure voltage limitation //20201104
#define NEW_STARTUP_FLUX_VOLT_LIMIT 3200           // range(800~12000), for new startup mode flux voltage limitation //20200319
#define NEW_STARTUP_FLUX_FEEDFORWARD_VALUE_CW 100  // range(0~600), for CW  feedforward flux control//20200319
#define NEW_STARTUP_FLUX_FEEDFORWARD_VALUE_CCW 100 // range(0~600), for CCW feedforward flux control//20200319

#ifdef ENABLE_1_SHUNT_R_TO_MEASURE_3_PHASE_CURRENT // 20200319
#define SMO_Kslf_MAX_VALUE_CW_NUSTARTUP 3600       // recommended value (300~6000), range (1~32767), for new startup mode,direction =CW
#define SMO_Kslf_MIN_VALUE_CW_NUSTARTUP 1200       // recommended value (100~4000), range (1~32767), for new startup mode,direction =CW

#define SMO_Kslf_MAX_VALUE_CCW_NUSTARTUP 3600 // recommended value (300~6000), range (1~32767), for new startup mode,direction =CCW
#define SMO_Kslf_MIN_VALUE_CCW_NUSTARTUP 1200 // recommended value (100~4000), range (1~32767), for new startup mode,direction =CCW
#else
#define SMO_Kslf_MAX_VALUE_CW_NUSTARTUP 4200 // 4200 //recommended value (300~6000), range (1~32767), for new startup mode,direction =CW
#define SMO_Kslf_MIN_VALUE_CW_NUSTARTUP 1200 // 1600 //recommended value (100~4000), range (1~32767), for new startup mode,direction =CW

#define SMO_Kslf_MAX_VALUE_CCW_NUSTARTUP 4200 // 4200 //recommended value (300~6000), range (1~32767), for new startup mode,direction =CCW
#define SMO_Kslf_MIN_VALUE_CCW_NUSTARTUP 1200 // 1600 //recommended value (100~4000), range (1~32767), for new startup mode,direction =CCW
#endif

#define PI_CURRENT_Vq_Kp_VALUE_CW_NUSTARTUP 32000 // for CW NEW_STARTUP_MODE current loop q axis PI Kp value setup //20200320
#define PI_CURRENT_Vd_Kp_VALUE_CW_NUSTARTUP 26000 // for CW NEW_STARTUP_MODE current loop d axis PI Kp value setup //20200320

#define PI_CURRENT_Vq_Kp_VALUE_CCW_NUSTARTUP 30000 // for CCW NEW_STARTUP_MODE current loop q axis PI Kp value setup //20200320
#define PI_CURRENT_Vd_Kp_VALUE_CCW_NUSTARTUP 16000 // for CCW NEW_STARTUP_MODE current loop d axis PI Kp value setup //20200320

#define SMO_I_DEFINE_NUSTARTUP_CW SMO_I_STATE_MUL1  // SMO_I_STATE_DIV4,SMO_I_STATE_DIV3,SMO_I_STATE_DIV2,SMO_I_STATE_MUL1,SMO_I_STATE_MUL3,SMO_I_STATE_MUL4 //20200727
#define SMO_I_DEFINE_NUSTARTUP_CCW SMO_I_STATE_MUL1 // SMO_I_STATE_DIV4,SMO_I_STATE_DIV3,SMO_I_STATE_DIV2,SMO_I_STATE_MUL1,SMO_I_STATE_MUL3,SMO_I_STATE_MUL4 //20200727

#define IalphaIbeta_CONTINUOUS_PROCESS_TIME 20 // unit:10ms,20 means every 200ms close in one stage for smo current continuous process after new startup entering close loop//20200803

#define ENABLE_BACK_TO_NEW_STARTUP_MODE_WHEN_LOW_SPEED                          // if enable, it will back to new startup mode if speed below the define "BACK_TO_NEW_STARTUP_MODE_SPEED"
#define BACK_TO_NEW_STARTUP_MODE_SPEED (TARGET_SPEED_1 - (TARGET_SPEED_1 >> 1)) // if speed under this in close loop mode, it will back to new startup mode

#define NEW_STARTUP_OVER_SPEED_CW ((TARGET_SPEED_7 * 2) / 3)  // new startup over speed define in  CW//20200611
#define NEW_STARTUP_OVER_SPEED_CCW ((TARGET_SPEED_7 * 2) / 3) // new startup over speed define in CCW//20200611

#ifdef ENABLE_NEW_STARTUP_METHOD
#define ENABLE_NEW_STARTUP_METHOD_FOR_DOWNWIND_STARTUP                   // 20190626, enable motor startup in downwind status
#define ENABLE_NEW_STARTUP_METHOD_FOR_AGAINST_WIND_STARTUP_AFTER_STOPPED // 20190626,restart motor use new startup method,for motor has stopped by open loop mode

/*if both of below two define are not enable, it will directly enter close loop and speed down to a defined speed then enter open loop and speed down to zero in high speed against wind*/
/*if reverse speed down to zero, it will use new startup method to restartup motor 20200803 */
//#define ENABLE_NEW_STARTUP_METHOD_FOR_HIGH_SPEED_AGAINST_WIND_STARTUP  //20200811//restart motor use new startup method directly when in high speed against wind
//#define ENABLE_NEW_STARTUP_METHOD_FOR_LOW_SPEED_AGAINST_WIND_STARTUP // 20200811//restart motor use new startup methodd  when in low speed against wind status

/*When receive reverse command from user,can enable this define to use new startup after reverse speed down to low speed */
//#define ENABLE_NORMAL_REVERSE_COMMAND_USE_NEW_STARTUP_AT_LOW_SPEED //20200811//use new startup method after close loop speed down to a defined speed at reverse direction
#endif

//-----normally,if enable the define "ENABLE_NEW_STARTUP_METHOD" , the Open loop parameters will be useless----------------------//20190626
//-----exception :if enable the define "ENABLE_NEW_STARTUP_METHOD" & "ENABLE_NEW_STARTUP_METHOD_AFTER_ALIGNMENT_AND_OPEN_LOOP_MODE", the align and open loop mode will be used //20190626
#ifdef ENABLE_NEW_STARTUP_METHOD
//#define ENABLE_NEW_STARTUP_METHOD_AFTER_ALIGNMENT_AND_OPEN_LOOP_MODE //if enable this define & "ENABLE_NEW_STARTUP_METHOD", new startup method will be used after align and open loop mode//20190626
#define OPEN_LOOP_SPEED_TO_ENTER_NEW_STARTUP_MODE 120 // unit:RPM, if open loop mode speed over this setup, it will enter new startup mode//20190626
#endif

#ifdef ENABLE_NEW_STARTUP_METHOD
//#define ENABLE_NEW_STARTUP_METHOD_AFTER_ALIGNMENT_MODE //if enable this define & "ENABLE_NEW_STARTUP_METHOD", new startup method will be used after alignment mode//20190626
#endif

//************************************************************************************************************************************************************
//-----Add OPEN LOOP MODE Between IPD1,2,3 mode and New startup Mode------------------------------------------------------------------------------------------
//************************************************************************************************************************************************************
////if enable IPD1 function, user can also run open loop mode before run in new startup mode-----------------------------------------------
////Before enable this define,
////please disable  "ENABLE_ROTOR_IPD2_FUNCTION" + "ENABLE_ROTOR_IPD3_FUNCTION"
////Consider turning off "ENABLE_NEW_STARTUP_METHOD_FOR_DOWNWIND_STARTUP","ENABLE_NEW_STARTUP_METHOD_FOR_AGAINST_WIND_STARTUP_AFTER_STOPPED"
////Consider turning off "ENABLE_NEW_STARTUP_METHOD_FOR_LOW_SPEED_AGAINST_WIND_STARTUP"
////it must enable "ENABLE_ROTOR_IPD1_FUNCTION" +�@��ENABLE_NEW_STARTUP_METHOD_AFTER_ALIGNMENT_AND_OPEN_LOOP_MODE��+ "ENABLE_NEW_STARTUP_METHOD"
////and need setup "#define OPEN_LOOP_SPEED_TO_ENTER_NEW_STARTUP_MODE   120 " to define open loop mode enter new startup mode's speed

//----------------------------------------------------------------------------------------------------------------------------------
//#define ENABLE_OPEN_LOOP_AFTER_IPD1_AND_THEN_NEW_STARTUP_MODE //enale IPD1->OPEN LOOP->NEW_STARTUP->CLOSE LOOP process //20191023
//----------------------------------------------------------------------------------------------------------------------------------

////if enable IPD2 function, user can also run open loop mode before run in new startup mode-------------------------------------------------
////Before enable this define,
////please disable  "ENABLE_ROTOR_IPD1_FUNCTION" + "ENABLE_ROTOR_IPD3_FUNCTION"
////Consider turning off "ENABLE_NEW_STARTUP_METHOD_FOR_DOWNWIND_STARTUP","ENABLE_NEW_STARTUP_METHOD_FOR_AGAINST_WIND_STARTUP_AFTER_STOPPED"
////Consider turning off "ENABLE_NEW_STARTUP_METHOD_FOR_LOW_SPEED_AGAINST_WIND_STARTUP"
////it must enable "ENABLE_ROTOR_IPD2_FUNCTION" +�@��ENABLE_NEW_STARTUP_METHOD_AFTER_ALIGNMENT_AND_OPEN_LOOP_MODE��+ "ENABLE_NEW_STARTUP_METHOD"
////and need setup "#define OPEN_LOOP_SPEED_TO_ENTER_NEW_STARTUP_MODE   120 " to define open loop mode enter new startup mode's speed

//----------------------------------------------------------------------------------------------------------------------------------
//#define ENABLE_OPEN_LOOP_AFTER_IPD2_AND_THEN_NEW_STARTUP_MODE //enale IPD2->OPEN LOOP->NEW_STARTUP->CLOSE LOOP process //20191024
//----------------------------------------------------------------------------------------------------------------------------------

////if enable IPD3 function, user can also run open loop mode before run in new startup mode-----------------------------------------------
////Before enable this define,
////please disable  "ENABLE_ROTOR_IPD1_FUNCTION" + "ENABLE_ROTOR_IPD2_FUNCTION"
////Consider turning off "ENABLE_NEW_STARTUP_METHOD_FOR_DOWNWIND_STARTUP","ENABLE_NEW_STARTUP_METHOD_FOR_AGAINST_WIND_STARTUP_AFTER_STOPPED"
////Consider turning off "ENABLE_NEW_STARTUP_METHOD_FOR_LOW_SPEED_AGAINST_WIND_STARTUP"
////it must enable "ENABLE_ROTOR_IPD3_FUNCTION" +�@��ENABLE_NEW_STARTUP_METHOD_AFTER_ALIGNMENT_AND_OPEN_LOOP_MODE��+ "ENABLE_NEW_STARTUP_METHOD"
////and need setup "#define OPEN_LOOP_SPEED_TO_ENTER_NEW_STARTUP_MODE   120 " to define open loop mode enter new startup mode's speed

//----------------------------------------------------------------------------------------------------------------------------------
//#define ENABLE_OPEN_LOOP_AFTER_IPD3_AND_THEN_NEW_STARTUP_MODE //enale IPD3->OPEN LOOP->NEW_STARTUP->CLOSE LOOP process //20191024
//----------------------------------------------------------------------------------------------------------------------------------

//************************************************************************************************************************************************************
//-----Alignment mode, Open loop mode parameters-------------------------------------------------------------------------------------------------------------
//-----define Rotor Alignment mode parameters----------------------------------------------------------------------------------------------------------------
//-----user can bypass alignment mode if both alignment parameters "OPEN_LOOP_ALIGNMENT_TIME" and "OPEN_LOOP_ALIGNMENT_CURRENT" be filled with 0
//-----if enable the define "ENABLE_ROTOR_IPD1_FUNCTION", the rotor alignment mode will be useless----------------//20190626
//-----if enable the define "ENABLE_ROTOR_IPD2_FUNCTION", the rotor alignment mode will be useless----------------//20190626
//-----if enable the define "ENABLE_NEW_STARTUP_METHOD" only, the rotor alignment mode will be useless------------//20190626
//-----exception : if enable the define "ENABLE_NEW_STARTUP_METHOD" & "ENABLE_NEW_STARTUP_METHOD_AFTER_ALIGNMENT_MODE", the rotor alignment mode will be used //20190626

// if both "OPEN_LOOP_ALIGNMENT_TIME" set to 0 and "OPEN_LOOP_ALIGNMENT_CURRENT " set to 0, it means bypass the RUN_IN_ALIGNMENT_MODE
#define OPEN_LOOP_ALIGNMENT_TIME 200  // 200     //unit:10ms, define open loop alignment time, 20 means 200ms as alignment time//20190623
#define OPEN_LOOP_ALIGNMENT_CURRENT 2 // 2       //unit:0.1A define the current of rotor alignment//20190623

//-----define Open loop mode parameters----------------------------------------------------------------------------------------------------------------------
//-----normally,if enable the define "ENABLE_NEW_STARTUP_METHOD" , the Open loop parameters will be useless----------------------//20190626
//-----exception :if enable the define "ENABLE_NEW_STARTUP_METHOD" & "ENABLE_NEW_STARTUP_METHOD_AFTER_ALIGNMENT_AND_OPEN_LOOP_MODE", the align and open loop mode will be used //20190626

//----------------------------------------------------------------------------------------------------------------------
#define OPEN_LOOP_TARGET_SPEED (TARGET_SPEED_7 / 4) // unit:RPM,define open loop target speed, then will enter close loop control
#define OPEN_LOOP_RAMP_UP_SPEED_SLOP1 200           // 30  //unit:RPM/sec,range(1~1000),acceleration slop in open loop 1st stage
#define OPEN_LOOP_RAMP_UP_SPEED_SLOP2 300           // 60  //unit:RPM/sec,range(1~1000),acceleration slop in open loop 2nd stage
#define OPEN_LOOP_RAMP_UP_SPEED_SLOP3 400           // 90  //unit:RPM/sec,range(1~1000),acceleration slop in open loop 3th stage

#define OPEN_LOOP_RAMP_DOWN_SPEED_SLOP1 30 // unit:RPM/sec,range(1~1000),deacceleration slop in open loop 1st stage
#define OPEN_LOOP_RAMP_DOWN_SPEED_SLOP2 45 // unit:RPM/sec,range(1~1000),deacceleration slop in open loop 2nd stage
#define OPEN_LOOP_RAMP_DOWN_SPEED_SLOP3 90 // unit:RPM/sec,range(1~1000),deacceleration slop in open loop 3th stage

#define RAMP_UP_CHANGE_TO_SLOP2_SPEED 15 // unit:RPM,define the open loop threshold speed for ramp up slop1 change to slop2
#define RAMP_UP_CHANGE_TO_SLOP3_SPEED 90 // unit:RPM,define the open loop threshold speed for ramp up slop2 change to slop3

#define OPEN_LOOP_TARGET_CURRENT 20    // unit:0.1A define the target  torque current in open loop ramp up stage
#define OPEN_LOOP_INITIAL_CURRENT 25   // unit:0.1A,define the initial torque current in open loop ramp up stage
#define OPEN_LOOP_RAMP_DOWN_CURRENT 15 // unit:0.1A,define the torque current in open loop ramp down stage

#define OPEN_LOOP_RAMP_UP_CURRENT_SLOP1 4  // unit:0.1A/sec,range(1~40),increase how many phase current per second in open loop 1st stage
#define OPEN_LOOP_RAMP_UP_CURRENT_SLOP2 4  // unit:0.1A/sec,range(1~40),increase how many phase current per second in open loop 2nd stage
#define OPEN_LOOP_RAMP_UP_CURRENT_SLOP3 10 // unit:0.1A/sec,range(1~40),increase how many phase current per second in open loop 3th stage

#define SPEED_SLOP_MULTIPLE_TIME_FASTER_PARA 1 // unit:Multiple parameter for open and close loop RAMP_UP_SPEED_SLOP. if 2, it means double speed up at RAMP UP.//20200603
//************************************************************************************************************************************************************
//-----Close loop mode parameters---------------------------------------------------------------------------------------------------
//---24v nidec fan-------------------------------------------------------------------------------------------------------------
#define TARGET_SPEED_1 300  // 400 //unit: RPM, define the user's lowest  target speed
#define TARGET_SPEED_2 500  // unit: RPM, define the user's second  target speed
#define TARGET_SPEED_3 550  // unit: RPM, define the user's Third   target speed
#define TARGET_SPEED_4 600  // unit: RPM, define the user's 4th     target speed
#define TARGET_SPEED_5 700  // unit: RPM, define the user's 5th     target speed
#define TARGET_SPEED_6 800  // unit: RPM, define the user's 6th     target speed
#define TARGET_SPEED_7 1600 // 900 //unit: RPM, define the user's highest target speed

#define CLOSE_LOOP_RAMP_UP_SPEED_SLOP (TARGET_SPEED_7 / 6)   // unit:RPM/sec,range(1~1000),acceleration slop in close loop
#define CLOSE_LOOP_RAMP_DOWN_SPEED_SLOP (TARGET_SPEED_7 / 6) // unit:RPM/sec,range(1~1000),deceleration slop in close loop//20190626

#define CLOSE_LOOP_RAMP_DOWN_SPEED_ULTRA_LOW_SLOP 10        // unit:RPM/sec,range(1~1000),ultra low deceleration slop in close loop//20190626
#define CLOSE_LOOP_ULTRA_LOW_SPEED_DOWN_SPEED_THRESHOLD 200 // unit:RPM, if under this threshold speed, the speed down will use ultra low speed down slop//20190626

#define CLOSE_LOOP_SPEED_TO_STOP_PWM (TARGET_SPEED_1 + 100) //((OPEN_LOOP_TARGET_SPEED*4)/2) //unit: RPM,(must > OPEN_LOOP_TARGET_SPEED),if receive motor stop command, pwm will off when speed under this define

//-----Motor Forward/against wind startup parameters------------------------------------------------------------------------
#define DIRECT_ENTER_CLOSE_LOOP_MIN_SPEED (TARGET_SPEED_7 / 5) // unit:RPM,define the mini. bemf speed which can directly enter close loop when motor startup
#define DOWNWIND_OPEN_LOOP_TARGET_SPEED (TARGET_SPEED_7 / 5)   // unit:RPM,define the open loop into close loop's speed in low speed downwind startup status

//************************************************************************************************************************************************************
//-----1,2 Shunt R, OP AMPLIFIER, PI Control output limit , DC BUS protection parameters-------------------------------------------------------------------------------------------------
#define SHUNT_R_VALUE 3 // unit:milli ohm, 50 means 0.05 ohm

#ifdef ENABLE_1_SHUNT_R_TO_MEASURE_3_PHASE_CURRENT
#define CURRENT_AMPLIFICATION_FACTOR 50 // unit:0.1 amplification factor, 60 means 60*0.1 = 6 amplification factor
#else
#define CURRENT_AMPLIFICATION_FACTOR 50 // unit:0.1 amplification factor, 50 means 50*0.1 = 5 amplification factor
#endif

#define CURRENT_LIMIT 300    // unit: 0.1A, it will limit the maximum sine wave phase current in close loop operation
#define CURRENT_PROTECT 2000 // unit: 0.1A, it will stop the motor if sine wave phase current over this value

//-----I_SUM R parameters for totoal current measurement-------------------------------------------------------------------
#define ISUM_R_VALUE 5                       // unit:milli ohm, 40 means 0.04 ohm
#define ISUM_CURRENT_AMPLIFICATION_FACTOR 50 // unit:0.1 amplification factor, 50 means 50*0.1 = 5 amplification factor

//-----DC Bus voltage measurement------------------------------------------------------------------------------------------
#define VBUS_PULL_UP_R 470   // unit : 0.1K Ohm, 1000 means 100K Ohm //20181215
#define VBUS_PULL_DOWN_R 100 // unit : 0.1K Ohm, 100  means 10K  Ohm //20181215

#define ENABLE_DC_BUS_VOLTAGE_PROTECTION    // if enable, it will do DC BUS voltage check and protection
#define DC_BUS_OVER_VOLTAGE_LIMITATION 300  // unint : 100mV, 300 means 30V, if DC Bus voltage over this, it will stop motor
#define DC_BUS_UNDER_VOLTAGE_LIMITATION 110 // unint : 100mV, 125 means 12.5V, if DC Bus voltage unser this, it will stop motor
#define DC_BUS_OVER_VOLTAGE_HYSTERESIS 20   ////unint : 100mV, 20 means 2V as hysteresis voltage for over voltage happened//20190626
#define DC_BUS_UNDER_VOLTAGE_HYSTERESIS 5   ////unint : 100mV, 20 means 2V as hysteresis voltage for under voltage happened//20190626

#define DC_BUS_AVERAGE_LPF_PARA 4 // range(1~16),for DC BUS average voltage,digital low pass filter parameter,if 4, it means the weight of new result is 4/16//20200824
//-----Vd, Vq compensation for DC BUS voltage drop out-----------------------------------------------------------------------
//#define ENABLE_DC_BUS_DROP_VdVq_COMPENSATION //if enable, it will do Vd Vq compensation when DC BUS voltage is not stay in TARGET_DC_BUS_VOLTAGE(time spent:3us)//20200819
#define ENABLE_DC_BUS_AVERAGE_CALCULATION_IN_EVERY_PWM_PERIOD // if enable, it will do dc bus average voltage in every pwm cycle(time spent:2us)//20200824
#define TARGET_DC_BUS_VOLTAGE 240                             // unint : 100mV,it is for Vd Vq compensation if DC BUS is not stable//20200819

//************************************************************************************************************************************************************
//-----Motor SMO parameters -----------------------------------------------------------------------------------------------
//---24v nidec fan
#ifdef ENABLE_1_SHUNT_R_TO_MEASURE_3_PHASE_CURRENT
#define SMO_Kslf_MAX_VALUE 3600 // recommended value (300~6000), range (1~32767), for close loop mode
#define SMO_Kslf_MIN_VALUE 1200 // recommended value (100~4000), range (1~32767), for close loop mode
#else
#define SMO_Kslf_MAX_VALUE 4200 // 4200 //recommended value (300~6000), range (1~32767), for close loop mode
#define SMO_Kslf_MIN_VALUE 1200 // 1600 //recommended value (100~4000), range (1~32767), for close loop mode
#endif

#define SMO_Kslide 26000  // recommended value 26000, range (1~32767)
#define G_SMO_VALUE 19000 // recommended value 19000, range (1~32767)//20190626
#define F_SMO_VALUE 30000 // recommended value 30000, range (1~32767)//20190626

//-----Ialpha, Ibeta To SMO angle calculation--------------------------------------------------------------------------------------------------------------
#define SMO_I_DEFINE_CLOSE_LOOP SMO_I_STATE_MUL1 // SMO_I_STATE_DIV4,SMO_I_STATE_DIV3,SMO_I_STATE_DIV2,SMO_I_STATE_MUL1,SMO_I_STATE_MUL3,SMO_I_STATE_MUL4 //20200727

//-----Vd, Vq OUTPUT Limit---------------------------------------------------------------------------------------------------------------------------------
//(sqr(Vd) + sqr(Vq))<32768

#define Vq_PI_OUT_LIMIT_CW 30000 // range(0~32767),Setup the Vd, Vq PI output limitation //20201102
#define Vd_PI_OUT_LIMIT_CW 13000 // range(0~32767),Setup the Vd, Vq PI output limitation //20201102

//#define Vq_PI_OUT_LIMIT_CW                         26000  //range(0~32767),Setup the Vd, Vq PI output limitation //20201102
//#define Vd_PI_OUT_LIMIT_CW                         20000  //range(0~32767),Setup the Vd, Vq PI output limitation //20201102

//#define Vq_PI_OUT_LIMIT_CW                         24000  //range(0~32767),Setup the Vd, Vq PI output limitation //20201102
//#define Vd_PI_OUT_LIMIT_CW                         22000  //range(0~32767),Setup the Vd, Vq PI output limitation //20201102

#define Vq_PI_OUT_LIMIT_CCW Vq_PI_OUT_LIMIT_CW // range(0~32767),Setup the Vd, Vq PI output limitation //20201102
#define Vd_PI_OUT_LIMIT_CCW Vq_PI_OUT_LIMIT_CW // range(0~32767),Setup the Vd, Vq PI output limitation //20201102

//----Over modulation increase setup-----------------------------------------------------------------------------------------------------------------------
// if need use over modulation in 2 shunt R, user should disable the "#define ENABLE_SVPWM_T1T2_TO_LIMIT_PWM_MAX_DUTY"
// if need use over modulation in 1 shunt R, user should enable "#define ENABLE_LEFT_HALF_PWM_GET_IaIbIc_1_SHUNT_R "
#define OVM_INCREASE 0 // unit: 1/64, 1 means add additional 1/64 voltage to SVPWM, range(0~16)//20201005

//---2 shunt R use only, define enable SVPWM T1 T2 Calculation to limit the pwm max duty----------//20191231//20201005-----------------------------------------------
//---if enable, phase current waveform will be better in 2 shunt R only, but PWM output will be limited in "MAX_PWM_DUTY_PERCENTAGE_2SHUNT_R" maximum range.
//---if "OVM_INCREASE" !=0 in 2 shunt R, please disable "#define ENABLE_SVPWM_T1T2_TO_LIMIT_PWM_MAX_DUTY"//20201005
//#define ENABLE_SVPWM_T1T2_TO_LIMIT_PWM_MAX_DUTY    //if enable, it will add a MAX duty limitation for SVPWM T1,T2 calculation when in 2 shunt R status.

//----define the Id command value when user want do field weakening control-----------------------------------------------------------------------------
//----it will has no effect when enable "ENABLE_MTPA_CONTROL"------------
#define Id_COMMAND_VALUE 0                                  // range (0~+4000)//20201005
#define MIN_SPEED_DO_Id_COMMAND_CTRL (TARGET_MAX_SPEED / 2) // 20200303

//-----add phase lead angle function------------------------------------------------------------------------------------------------------------------------
//#define ENABLE_PHASE_LEAD_ANGLE_FUNCTION //for enable phase lead angle add to smo/PLL output angle.//20200914
#define PHASE_LEAD_ANGLE 0                                       // range(0~60), 1023 == 360degree.
#define MIN_SPEED_TO_ADD_PHASE_LEAD_ANGLE (TARGET_MAX_SPEED / 2) // mini.speed to add a phase lead angle//20200914

//************************************************************************************************************************************************************
//-----Motor current, speed PI parameters setup------------------------------------------------------------------------------------------------------------
//-----Motor FOC current PI parameters ------------------------------------------------------------------------------------
//---24v nidec fan
#define PI_CURRENT_Kp_VALUE 24000
#define PI_CURRENT_Ki_MAX_VALUE 5600
#define PI_CURRENT_Ki_MIN_VALUE 280

//---for 8cm big server fan
//#define PI_CURRENT_Kp_VALUE       3000//for server fan 24000
//#define PI_CURRENT_Ki_MAX_VALUE   5600
//#define PI_CURRENT_Ki_MIN_VALUE   280//560

//#define PI_CURRENT_Ki_MAX_VALUE   15000//for very low speed command
//#define PI_CURRENT_Ki_MIN_VALUE   15000//for very low speed command

//-----define current PI integral result divider for open loop enter close loop//20190123---------------------------------
#define CURRENT_PI_INTEGRAL_RESULT_DIVIDER_WHEN_ENTER_CLOSE_LOOP 1 // divider number, if define 32,it means 1/32

//-----Motor speed PI parameters ------------------------------------------------------------------------------------------
#define PI_SPEED_Kp_VALUE 24000
#define PI_SPEED_Ki_VALUE 1500

//*************************************************************************************************************************
//-----Motor Error code define---------------------------------------------------------------------------------------------
#define MAX_ERROR_ACCUMULATIVE_TOTAL 10 // if error counter over or equal this value, the motor will not restart again until receive off command
#define ERROR_RESTART_WAIT_TIME 5       // unit :100ms, after a error happened, waiting this time then can restart motor

#define MOTOR_SPEED_ERROR 1
#define MOTOR_PHASE_CURRENT_ERROR 2
#define MOTOR_OVER_CURRENT 3
#define MOTOR_ROTATION_INVERSE_ERROR 4
#define MOTOR_OVER_UNDER_VOLTAGE_ERROR 5
#define NEW_STARTUP_MODE_OVER_TIME_ERROR 6 // 20190626
#define MOTOR_DCBUS_OVER_CURRENT_ERROR 7   // 20190626
#define MOTOR_LACK_PHASE_ERROR 8           // 20190626

/**************************************************************************************************************************
** Advance parameters setup : common user do not change the below parameters
**************************************************************************************************************************/
//----Enable/Disable TIM1_CC4 to trigger ADC for 2 shunt R current sensing-------------------------------------------------
//#define ENABLE_HARDWARE_DIVIDER // if define, use internal hardware divider. if not define, it will use software divider
//!!?????????
//----define OVER CURRENT PROTECT--------------------------------------------------------------------------------------------------------------------------------------------------
//#define ENABLE_COMP1_AND_iVREF_INTER_CONNECT_TIM1BKIN_DO_OVER_CURRENT_PROTECT // if use internal COMPARATOR and internal Vref 1.2V with TIM1BKIN brake PWM,user can enable this define
#define ENABLE_TIM1BKIN_INPUT_PIN_ONLY_DO_OVER_CURRENT_PROTECT // if use external COMPARATOR, user can enable this define (input to PB12 pin)

//----for MM32L073 Only,define the internal comparator inverting input (INM) 1,3/4,1/2,1/4Vref voltage, internal Vref =1.2V --------------------------------------------------------
#define INTERNAL_COMPARATOR_INM_REF_VOLTAGE COMP_InvertingInput_VREFINT // COMP_InvertingInput_1_4VREFINT//COMP_InvertingInput_1_2VREFINT//COMP_InvertingInput_3_4VREFINT

//----define the internal comparator inverting input (INM) 1/20,2/20,1/2,...16/20 VDDA(5V) voltage ------------------------------------------------------------------------------------------------
//(3_24) real is (3-2)/20 of 5V = 0.25V(MIN),...(6_24) real is (6-2)/20 of 5V = 1V.....(18_24) real is (18-2)/20 of 5V = 4V(MAX)
#define INTERNAL_COMPARATOR_INM_REF_CRV_VOLTAGE COMP_CRV_Level_6_20 // COMP_CRV_Level_3_24(0.25V),COMP_CRV_Level_4_24(0.5V).......COMP_CRV_Level_18_24

//----define TIM1BKIN input High or Low to make PWM OUTPUT OFF---------------------------------------------------------------------------------------------------------------------
#define BREAK_POLARITY TIM_BreakPolarity_High // TIM_BreakPolarity_Low//if define High, TIM1BKIN receive high signal will let Motor PWM output OFF

//---define POWER command input control to replace speed command input control//20191108-------------------------------------------------------------------------------------------
//#define ENABLE_POWER_CMD_INPUT_CONTROL_TO_REPLACE_SPEED_CMD_INPUT_CONTROL //if enable, it means user want use power command input control to replace the speed command input control
#define INCREASE_02W_EVERY_X_MS 50                          // unit: 1ms, how many ms to increase/decrease 0.2W for target power command in close loop.(for power command ramp up/down slop control)
#define UNDO_SPEED_LIMIT_TIME_IF_TARGET_POWER_CMD_CHANGED 4 // if 4, mean 4*50ms = 200ms, if target power command changed, it will not do speed limitation for 200ms//20191108
#define TARGET_MAX_POWER TARGET_POWER_7                     // unit: 0.1W,  for max. target power setup

#ifdef ENABLE_POWER_CMD_INPUT_CONTROL_TO_REPLACE_SPEED_CMD_INPUT_CONTROL
#define ENABLE_MAX_SPEED_LIMIT_IN_POWER_CMD_INPUT_CONTROL     // if use power command input control, user can enable this to limit motor run under the max speed range
#define HYSTERESIS_OF_MAX_SPEED_LIMIT (TARGET_MAX_SPEED / 20) // hysteresis value for motor keep in the max speed command control when real speed > TARGET_MAX_SPEED
// the speed limitation setup use the below parameter in power command input control
//#define TARGET_MAX_SPEED    TARGET_SPEED_7  //unit:RPM, max. speed limitation

#define ENABLE_MIN_SPEED_LIMIT_IN_POWER_CMD_INPUT_CONTROL     // if use power command input control, user can enable this to limit motor run over the defined min. speed
#define HYSTERESIS_OF_MIN_SPEED_LIMIT (TARGET_MIN_SPEED / 10) // hysteresis value for motor keep in the min. speed command control when real speed < TARGET_MIN_SPEED
// the speed limitation setup use the below parameter in power command input control
//#define TARGET_MIN_SPEED    TARGET_SPEED_1  //unit:RPM, max. speed limitation
#endif

#define TARGET_POWER_1 50  // unit: 0.1W, define the user's target power
#define TARGET_POWER_2 100 // unit: 0.1W, define the user's target power
#define TARGET_POWER_3 200 // unit: 0.1W, define the user's target power
#define TARGET_POWER_4 300 // unit: 0.1W, define the user's target power
#define TARGET_POWER_5 400 // unit: 0.1W, define the user's target power
#define TARGET_POWER_6 500 // unit: 0.1W, define the user's target power
#define TARGET_POWER_7 600 // unit: 0.1W, define the user's target power

//---define Iq command input control to replace speed command input control//20191122-------------------------------------------------------------------------------------------
//#define ENABLE_Iq_CMD_INPUT_CONTROL_TO_REPLACE_SPEED_CMD_INPUT_CONTROL //if enable, it means user want use Iq command input control to replace the speed command input control
#define Iq_CMD_INPUT_RAMP_UP_CURRENT_SLOP 10                   // unit:0.1A/sec,range(1~400),increase how many torque current per second in Iq command input control//20191122
#define UNDO_SPEED_LIMIT_TIME_IF_TARGET_Iq_Input_CMD_CHANGED 4 // if 4, mean 4*50ms = 200ms, if target Iq command changed, it will not do speed limitation for 200ms//20191122

#ifdef ENABLE_Iq_CMD_INPUT_CONTROL_TO_REPLACE_SPEED_CMD_INPUT_CONTROL
#define ENABLE_MAX_SPEED_LIMIT_IN_Iq_CMD_INPUT_CONTROL        // if use Iq command input control, user can enable this to limit motor run under the max speed range
#define HYSTERESIS_OF_MAX_SPEED_LIMIT (TARGET_MAX_SPEED / 20) // hysteresis value for motor keep in the max speed command control when real speed > TARGET_MAX_SPEED
// the speed limitation setup use the below parameter in Iq command input control
//#define TARGET_MAX_SPEED    TARGET_SPEED_7  //unit:RPM, max. speed limitation

#define ENABLE_MIN_SPEED_LIMIT_IN_Iq_CMD_INPUT_CONTROL        // if use Iq command input control, user can enable this to limit motor run over the defined min. speed
#define HYSTERESIS_OF_MIN_SPEED_LIMIT (TARGET_MIN_SPEED / 10) // hysteresis value for motor keep in the min. speed command control when real speed < TARGET_MIN_SPEED
// the speed limitation setup use the below parameter in power command input control
//#define TARGET_MIN_SPEED    TARGET_SPEED_1  //unit:RPM, max. speed limitation
#endif

#define TARGET_TORQUE_CURRENT_1 40  // unit: 0.1A, define the user's target torque current
#define TARGET_TORQUE_CURRENT_2 50  // unit: 0.1A, define the user's target torque current
#define TARGET_TORQUE_CURRENT_3 60  // unit: 0.1A, define the user's target torque current
#define TARGET_TORQUE_CURRENT_4 70  // unit: 0.1A, define the user's target torque current
#define TARGET_TORQUE_CURRENT_5 80  // unit: 0.1A, define the user's target torque current
#define TARGET_TORQUE_CURRENT_6 90  // unit: 0.1A, define the user's target torque current
#define TARGET_TORQUE_CURRENT_7 100 // unit: 0.1A, define the user's target torque current

#ifndef ENABLE_POWER_CMD_INPUT_CONTROL_TO_REPLACE_SPEED_CMD_INPUT_CONTROL
#ifndef ENABLE_Iq_CMD_INPUT_CONTROL_TO_REPLACE_SPEED_CMD_INPUT_CONTROL
#define ENABLE_SPEED_CMD_INPUT_CONTROL
#endif
#endif

//----define ISUM current measurement enable or not-------------------------------------------------------------------------------------
#define ENABLE_ISUM_MEASUREMENT // enable this to measure total current and power consumption

//----define Maximum Power Consumption Control(for speed command input control use only, not for power / Iq command input---------------
#ifdef ENABLE_SPEED_CMD_INPUT_CONTROL
#ifdef ENABLE_ISUM_MEASUREMENT
//#define ENABLE_MAX_POWER_LIMIT      //for max. power consumption limitation control enable or not
#define ENABLE_MAX_CURRENT_LIMIT // for max. DC BUS current limitation control enable or not
#endif
#endif

#define USE_MEASURED_DC_BUS_VOLTAGE_TO_GET_POWER // if enable,it will use measured dc bus voltage to calculate power consumption. if not, it will use fixed DC_BUS_VOLTAGE parameter
#define DC_BUS_VOLTAGE 240                       // unit: 0.1V, define the dc bus voltage, 100 means 10V. it will be used if not define "USE_MEASURED_DC_BUS_VOLTAGE_TO_GET_POWER"

//----for MAX DCBUS CURRENT LIMIT //20190626--------------------------------------------------------------------------------------------------------------
#define MAX_DCBUS_CURRENT_PROTECTION 8000 // unit:1mA, define the dc bus max current protection,2000 means 2.0A, if over it, motor will be stopped and show error
#define MAX_DCBUS_CURRENT_LIMIT 5000      // unit:1mA, define the dc bus max current limitation,1500 means 1.5A //20190626
#define MIN_CURRENT_LIMIT_RESOLUTION 40   // 10 means 10mA as minimum current limitation resolution,if over max.current 20mA,it means 20mA/10mA =2RPM Speed CMD Decrease every 50ms//20190606

#define SPEED_CMD_DEC_RPM_FOR_POWER_LIMIT 1             // 5 means max. 5 rpm decrease every 50ms for speed command, when power over limitation //20190605
#define CALIBRATION_OFFSET_VOLTAGE (2)                  // 2 means 2*1.25 =2.5mV Offset for calibration OP Amplifier output//20190606
#define UNDO_POWER_LIMIT_TIME_IF_TARGET_SPEED_CHANGED 4 // if 4, mean 4*50ms = 200ms, if target speed command changed, it will not do power limitation for 200ms//20190606

//----for MAX POWER LIMIT-----------------------------------------------------------------------------------------------------------------------
//#define MAX_POWER_LIMIT                     400 //unit: 0.1W, define the max power limitation, 240 means it will limit power consumption in 24W
//#define MIN_POWER_LIMIT_RESOLUTION          10  //10 means 1.0W as minimum power limitation resolution,if over max.power 2W,it means 2W/1W =2RPM Speed CMD Decrease every 50ms//20190606

//#define SPEED_CMD_DEC_RPM_FOR_POWER_LIMIT  5  //5 means max. 5 rpm decrease every 50ms for speed command, when power over limitation //20190605
//#define CALIBRATION_OFFSET_VOLTAGE        (2) //2 means 2*1.25 =2.5mV Offset for calibration OP Amplifier output//20190606
//#define UNDO_POWER_LIMIT_TIME_IF_TARGET_SPEED_CHANGED  4  //if 4, mean 4*50ms = 200ms, if target speed command changed, it will not do power limitation for 200ms//20190606

//----define the against wind startup motor parameters-------------------------------------------------------------------------
// for restart the motor when motor is in againstwind status
#define AGAINST_WIND_RESTART_RAMP_UP_SLOP_DIVIDE_PARA 2                       // define open loop speed ramp up slop divider para., 2 means 1/2  normal ramp up speed acceleration//20180807
#define AGAINST_WIND_CLOSE_TO_OPEN_LOOP_SPEED ((OPEN_LOOP_TARGET_SPEED) + 60) //(need > OPEN_LOOP_TARGET_SPEED), define the close to open loop speed in reverse and high speed status//20180807
#define AGAINST_WIND_OPEN_TO_CLOSE_TARGET_SPEED (OPEN_LOOP_TARGET_SPEED)      //(need < OPEN_LOOP_TARGET_SPEED),define the open to close speed after motor is stopped in against wind//20180807
#define K_AGAINST_WIND_OPL 2                                                  // suggest range 1~4, modulate the open loop initial Iq integral//20180807

//----define the initial parameters when the motor is stopped by reverse action in against wind -----------------------------
#define RESTARTUP_INITIAL_Iq_DIVIDE_PARA 2         // 4 means 1/4 Motor1.command.s16OpenLoopInitIq value for initializing the Iq_PI.s32IntegralResult//20190408
#define PHASE_LEAD_ANGLE_IN_AGAINST_WIND_RESTART 0 // 45 means 45 degree of 360 degree for phase lead (32768/360 = 91)//20190408

//----define the down wind startup motor parameters-------------------------------------------------------------------------
// for restart the motor at down wind low speed status
#define DOWN_WIND_RESTART_RAMP_UP_SLOP_DIVIDE_PARA 2 // open loop speed up slop setup, 2 means 1/2  normal ramp up speed acceleration//20180807

//----define phase current digital filter parameter for 1 shunt R phase current only------------------------------------------------------------------
#define PHASE_CURRENT_AVERAGE_WEIGHT 1         // Option(1,2,4),only for 1 shunt R.if 2, it means this time new phase current measurement result is 1/2 weight.
#define FORCE_TO_FILTER4_MAX_RPM_1_SHUNT_R 600 // unit: RPM, new measurement phase current will weight to 1/4, if speed below this speed in 1 shunt R

//----define the default zero voltage of BEMF A, BEMF B when WL MOSFET is ON---------------------------------------------------------------------------
#define DEFAULT_ZERO_BEMF_VOLTAGE 1 // unit:0.1V,define the default zero voltage of BEMF A, BEMF B when WL MOSFET is on

//----enable /disable the angle compensation in the beginning of close loop----------------------------------------------------------------------------
#define ENABLE_OPEN_TO_CLOSE_ADVANCE_GRADUALLY // if define,the motor driving angle is gradually from the open loop angle to SMO angle at the beginning of close loop

// when motor driving angle is gradual from the open loop angle to SMO angle, it define how many pwm cycles close to 0.35 degree
// difference angle between open loop and SMO angle
#define PWM_CYCLES_CLOSE_TO_035_DEGREE 12 // if define 12, it means every 12 pwm cycles close to 0.35 degree until the difference angle is 0

//----define Fix Iq command action in the beginning of close loop--------------------------------------------------------------------------------------
#define FIX_Iq_COMMAND_TIME 5                            // unit :100ms, it define a the fix Iq command time when first time into the close loop
#define GRADUAL_Iq_COMMAND_TIME 3                        // unit :100ms, after FIX_Iq_COMMAND_TIME, it need a gradual time for from FIX_Iq_COMMAND to close loop Iq command
#define FIX_Iq_COMMAND (OPEN_LOOP_RAMP_UP_TARGET_Iq / 4) // range (0~32767), it will fix this Iq command during a FIX_Iq_COMMAND_TIME time

//----define the minimum Iq command in close loop, normally the Iq command is negative number-----------------------------------------------------------
#define LIMIT_MINIMUM_Iq_COMMAND (100) // range (-1000~+1000), it limit the minimum Iq command

//----define the BEMF detect time, and others (option "ENABLE_LOWER_3_ARM_UL_VL_WL_ON_BRAKE_VERY_LOW_SPEED"to brake motor before startup)----------------
#define ENABLE_EVERY_200us_DETECT_BEMF_SPEED_AND_DIRECTECTION_ONCE // 20200803B
//#define ENABLE_EVERY_1ms_DETECT_BEMF_SPEED_AND_DIRECTECTION_ONCE //20200803B

#define BEMF_DETECT_LIMIT_TIME 600 // unit: 1ms, it define the maximum time to detect BEMF's speed and direction//20200225
#define BEMF_SPEED_AS_STANDSTILL 8 // unit: rpm, if bemf speed under this define, it will be treated as standstill startup, and can use brake to stop motor

//---define lower 3 armed (UL,VL,WL) brake function for bemf very low speed startup//20200225-------------------------------------------------------------
//---the bemf very low's speed is define as "BEMF_SPEED_AS_STANDSTILL" parameter,if motor is rotating before startup,and bemf speed below this define,
// it can be treated as standstill status or use break to stop motor before startup(by enable"ENABLE_LOWER_3_ARM_UL_VL_WL_ON_BRAKE_VERY_LOW_SPEED")
//#define ENABLE_LOWER_3_ARM_UL_VL_WL_ON_BRAKE_VERY_LOW_SPEED      //if enable, it will use UL,VL,WL Turn on to brake the motor at bemf speed very low's startup status
#define K_PARAMETER_OF_UL_VL_WL_BRAKE_VERY_LOW_SPEED 10 // Brake time = 10ms * (LOWER_3_ARM_BRAKE_MIN_TIME + (BEMF_SPEED /K_PARAMETER_OF_UL_VL_WL_BRAKE))
#define LOWER_3_ARM_BRAKE_MIN_TIME_VERY_LOW_SPEED 300   // unit:10ms, define the minimum time of brake time for bemf speed very low startup

//---define lower 3 armed (UL,VL,WL) brake function for against wind startup//20200227--------------------------------------------------------------------
//#define ENABLE_LOWER_3_ARM_UL_VL_WL_ON_BRAKE_AGAINST_WIND        //if enable, it will use UL,VL,WL Turn on to brake the motor at against wind startup status
#define K_PARAMETER_OF_UL_VL_WL_BRAKE_AGAINST_WIND 10 // Brake time = 10ms * (LOWER_3_ARM_BRAKE_MIN_TIME + (BEMF_SPEED /K_PARAMETER_OF_UL_VL_WL_BRAKE))
#define LOWER_3_ARM_BRAKE_MIN_TIME_AGAINST_WIND 300   // unit:10ms, define the minimum time of brake time

//----define the maximum difference voltage between BEMF A,B, if under this threshold voltage, the motor is in standstill status before startup---------
#ifdef ENABLE_ROTOR_IPD1_FUNCTION
#define BEMF_COMPARATOR_HYSTERESIS 100        // unit: mV,  it define the digital comparator's hysteresis voltage
#define BEMF_STANDSTILL_THRESHOLD_VOLTAGE 200 // unit: mV,  it define the standstill if BEMFA,B difference voltage under this limitation
#else
#define BEMF_COMPARATOR_HYSTERESIS 100        // unit: mV,  it define the digital comparator's hysteresis voltage
#define BEMF_STANDSTILL_THRESHOLD_VOLTAGE 200 // unit: mV,  it define the standstill if BEMFA,B difference voltage under this limitation
#endif

//----define the Dead time-------------------------------------------------------------------------------------------------------------------------------
#define DEAD_TIME_SETUP 10 // unit : 20.83ns (at system clock 48MHz),range(0~127)

//----define the K parameter for the initial Vq when motor startup in bemf high speed downwind/against wind status---------------------------------------
#define K_STARTUP_Vq_INITIAL_PARA 256 // range(1~512),the initial Vq integrator parameter,for motor startup direct into close loop status(in downwind or against wind startup)

//----Enable/disable force motor always in open loop mode OR new startup mode----------------------------------------------------------------------------
#define FORCE_NOT_STAY_IN_OPEN_LOOP_CMD 1   // if set to 0, it will default always keep motor running in open loop mode if user has enable open loop mode//20200309
#define FORCE_NOT_STAY_IN_NEW_STARTUP_CMD 1 // if set to 0, it will default always keep motor running in new startup mode, if user has enable new startup mode//20200309
#define DIVID_NEW_STARTUP_TARGET_CURRENT 8  // if 4, it means NEW_STARTUP_TARGET_CURRENT will be 1/4 when motor always keep in new startup mode//20200309

//----define the PVD(programmable voltage detector) voltage, softeware will reset MCU if VDD under this define voltage-------------------------------------
#define PVD_VOLTAGE PWR_PVDLevel_2V7 // 3V6 means 3.6V, (Option : 2V7,3V0,3V6,3V9,4V2...)

//----define enable/disable MOSFET WL pin turn on at CHECK_BEMF_MODE---------------------------------------------------------------------------------------
//#define  ENABLE_WL_ON_AT_CHECK_BEMF_MODE    //if define, it means enable WL turn on at CHECK BEMF MODE before motor startup and IPD
#define ENABLE_MINI_WL_ON_WIDTH_AT_CHECK_BEMF_MODE // if define, it means the WL on width will be minimize for reducing the vibration and sound noise
#define WL_ON_TIME_SETUP_AT_CHECK_BEMF_MODE 250    // unit: us, range(200~500), setup WL on time for every 1ms at CHECK_BEMF_MODE, BEMF U,V will be measured during this time and then off WL

//----if normally stop by PWM OFF, define a minimum time delay then can restart motor-----------------------------------------------------------------------
#define RESTART_WAIT_TIME_AFTER_PWM_OFF 0 // unit: 0.1second, 50 measns wait 5 second after PWM off, then can restart motor

//-----Motor Maximum Speed Calculation parameters setup-----------------------------------------------------------------------------------------------------
//#define ENABLE_MAX_120000RPM_SPEED_CALCULATION    //(2 poles)
#define ENABLE_MAX_24000RPM_SPEED_CALCULATION //(2 poles)

//-----2 PWM cycles do once FOC,SMO and others calculation Option------------------------------------------------------------------------------------------
//#define ENABLE_EVERY_2_PWM_CYCLES_DO_ONCE_FOC_SMO_CALCULATTION //if not define, do FOC & SMO Calculation in every PWM cycle.

//-----Enable CW TO CCW or CCW to CW Function By KEY1 INPUT------------------------------------------------------------------------------------------------
//#define ENABLE_MOTOR_REVERSIBLE_FUNCTION //20190504//20201112 change default to disable this

//-----Enable ERROR CODE show to LED-----------------------------------------------------------------------------------------------------------------------
#define ENABLE_ERROR_CODE_SHOW_TO_LED // 20190515

//-----Ialpha, Ibeta To SMO angle calculation--------------------------------------------------------------------------------------------------------------
//#define I_DIVIDE_4_TO_SMO    //Ialpha, beta Divide 4  input to SMO parameter //20190626
//#define I_DIVIDE_2_TO_SMO    //Ialpha, beta Divide 2  input to SMO parameter //20190626
#define I_MULTIPLY_TO_SMO 1 // range(1,2,4),if disable "I_DIVIDE_4_TO_SMO"and"I_DIVIDE_2_TO_SMO" , it will Multiply this setup value to Ialpha, beta for SMO parameter//20190626

//-----setup id integrator to 0 when in open loop mode----------------------------------------------------------------------------------------------------
#define ENABLE_Id_INTEGRATOR_TO_0_DURING_OPEN_LOOP // 20190626//set id integrator to 0 when in open loop mode

//----rotor alignment parameters advance setup------------------------------------------------------------------------------------------------------------
// if enable "ENABLE_ROTOR_ALIGN_EARLIER_FINISH_DETECTION", the OPEN_LOOP_ALIGNMENT_ANGLE need set to 0//20190623

//#define ENABLE_ROTOR_ALIGN_EARLIER_FINISH_DETECTION  //if enable, it will auto detect the rotor alignment has earlier finished or not//20190623
#define ALIGNMENT_READY_THRESHOLD 4        // unit:mV, for 12bit Iu current detect threshold, when in align mode (it will measure IU OP AMPLIFY OUTPUT)//20190623
#define DIVIDER_OF_ALIGN_INITIAL_CURRENT 4 // range(1,2,4),divider to control the current loop initial integral_result,(refer to OPEN_LOOP_ALIGNMENT_CURRENT)//20190623
#define OPEN_LOOP_ALIGNMENT_ANGLE 0        // ANGLE range(0~1023) equal to rotor (0~360degree), it means rotor 60degree = 171 ANGLE//20190623

//----define enable 3 shunt R's  Iw current measurement //20190626----------------------------------------------------------------------------------------
//#define ENABLE_3_SHUNT_R_CURRENT_MEASURE  //if not enable it, the current measure will be 1 or 2 shunt R, depend on define "ENABLE_1_SHUNT_R_TO_MEASURE_3_PHASE_CURRENT"

//----define lack phase check parameters //20190626------------------------------------------------------------------------------------------------------
//#define ENABLE_LACK_PHASE_DETECT_AND_PROTECT         //if enable, it will check there is a lack phase error or not, if yes,motor will be stop//20190626
#define LACK_PHASE_RATIO 4               // if average Ix phase current > average(1+ (Ratio/4))Iy, it will think there is a lack phase error//20190626
#define LACK_PHASE_DETECT_CYCLE_TIME 512 // unit:2ms, 2*LACK_PHASE_DETECT_CYCLE_TIME mili-second to do once checking lack phase.(Ia, Ib, Ic sum current calculation and compare)

//----define remove the BEMF detection before startup. if enable, user can remove BEMF detect circuit-----------------------------------------------------
//#define ENABLE_REMOVE_BEMF_DETECTION //20190912 //user must enable the "#define ENABLE_NEW_STARTUP_METHOD" before enable this define

//----define add TARGET_SPEED_1 speed can run in open loop mode-------------------------------------------------------------------------------------------
// This use  " OPEN_LOOP_RAMP_DOWN_CURRENT"  for torque current in open loop ramp down stage
//#define ENABLE_TARGET_SPEED_1_RUN_IN_OPEN_LOOP_MODE //20190912 user must enable the "#define ENABLE_NEW_STARTUP_METHOD" before enable this define

//---HFJ other optoin define------------------------------------------------------------------------------------------------------------------------------
//#define ENABLE_SQUARE_WAVE_HFJ                 //if enable, it will use square wave HFJ//20191001

//---define enable 5 segment svpwm------------------------------------------//20191207--------------------------------------------------------------------
//#define ENABLE_5_SEGMENT_SVPWM                              //if enable, the 7 segment svpwm will change to 5 segment svpwm
#define MINI_SPEED_FROM_7SEG_INTO_5SEG (TARGET_MAX_SPEED / 2) // define the speed enter into 5 segment svpwm. if motor speed under this define, it will run in 7 seg. svpwm

//---define enable PLL function for SMO rotor position estimation-----------//20191213--------------------------------------------------------------------
#define ENABLE_PLL_FOR_SMO_ROTOR_POSITION_ESTIMATION // if enable, it will enable PLL for smo rotor position estimation
#define LPF_DIGITAL_FILTER_PARA 8                    // 12 //digital low pass filter parameter,if 7, it means the weight of new result is 7/16
#define PLL_MINI_LOCK_TIME (PWM_FREQUENCY / 4)       // unit:pwm cycle, define the PLL need keep a stable status then enter locked status
#define PLL_MAX_DELTA_ERROR 8                        // unit: %, Delta error need under this define
#define WAIT_SMO_STABLE_TIME_FOR_PLL 250             // unit:ms for wait smo angle stable time after entering close loop//20200406
#define PLL_LOCK_OVER_TIME (PWM_FREQUENCY)           // unit:pwm cycle. define max. pll lock time, if over this, it will do pll relock action//20200408

#define SMO_PLL_SPEED_Kp_VALUE 5000 // 1500//3000//Motor smo+ pll estimated speed PI parameters
#define SMO_PLL_SPEED_Ki_VALUE 1500 // 500//1500 //Motor smo+ pll estimated speed PI parameters

//---define enable SMO add 2nd low pass filter------------------------------//20191231---------------------------------------------------------------------
//#define ENABLE_SMO_ADD_A_2nd_LPF    //if enable, it will add a 2nd low pass filter for SMO theta output
//#define ENABLE_SMO_ADD_A_3rd_LPF    //if enable, it will add a 3rd low pass filter for SMO theta output

//---sine table size option--------------//20200206--------------------------------------------------------------------------------------------------------
//#define ENABLE_1024_16BIT_SIN_TABLE  //enable 2K Bytes sine table
#define ENABLE_512_16BIT_SIN_TABLE // enable 1K Bytes sine table

//---define enable MTPA function--------------//20200303----------------------------------------------------------------------------------------------------
// if enable "ENABLE_MTPA_CONTROL", original Iq command will be replaced by s16IsCommand, and s16IsCommand = sqrt((Iq command*Iq command)+(Id command*Id command))
//"CURRENT_LIMIT" will be changed to limit the s16IsCommand max.value, and "MAX_DCBUS_CURRENT_LIMIT","MAX_POWER_LIMIT" will limit the power output
// Vsref = K_VUF*VDC/1.732, K_VUF = voltage utilization factor, K_VUF<1
// TARGET_MAX_SPEED need >= MTPA_CORNER_SPEED

//#define ENABLE_MTPA_CONTROL                             //enable MTPA (max torq per Ampere) function
#define MTPA_BETA_DEGREE 5                               // unit:degree, range (0~45degree), it can not out of range
#define MTPA_CORNER_SPEED ((TARGET_MAX_SPEED * 12) / 16) // unit:RPM,the threshold speed defined between constant torque and constant power area
#define MTPA_MAX_Id_CURRENT_LIMIT_ALL 10                 // unit:0.1A, for (field weakening Id command + MTPA Id command) max. current limit value setup
#define MTPA_PI_FW_Id_MAX_CURRENT_LIMIT 3                // unit:0.1A, for (field weakening Id command)) max. current limit value setup

#define ENABLE_CONST_POWER_Id_CMD_MODULATE_BY_Vsref // if enable, it will use Vsref to modulate Id command in constant power area
#define CONST_POWER_Vsref_VALUE 92                  // unit:%,define the max voltage utilization for field weakening in constant power area
#define MTPA_FW_Id_Kp_VALUE 3000                    // PI controller parameter for flux weakening in constant power area
#define MTPA_FW_Id_Ki_VALUE 300                     // PI controller parameter for flux weakening in constant power area

#ifndef ENABLE_CONST_POWER_Id_CMD_MODULATE_BY_Vsref
#define ENABLE_CONST_POWER_Id_CMD_MODULATE_BY_SPEED // if enable, it will use motor speed to modulate Id command in constant power area
#endif

//---Add smo angle digital filter option, it is not used after PLL locked or speed higher than defined--------------------------------//20200415-----------------------
//#define ENABLE_SMO_ANGLE_OUTPUT_ADD_A_LPF
#define SMO_ANGLE_FIR_LPF_PARA 16                      // range(1~16),digital low pass filter parameter,if 4, it means the weight of new result is 4/16
#define MAX_SPEED_FOR_SMO_ANGLE_FIR_LPF TARGET_SPEED_1 // unit:RPM, if over this speed, the smo angle FIR LPF will be disabled

//---Add smo bemf digital filter option,for smo angle only, it is not used after PLL locked or speed higher than defined--------------//20200415-----------------------
//#define ENABLE_SMO_BEMF_OUTPUT_ADD_A_LPF
#define SMO_BEMF_FIR_LPF_PARA 16                      // range(1~16),digital low pass filter parameter,if 4, it means the weight of new result is 4/16
#define MAX_SPEED_FOR_SMO_BEMF_FIR_LPF TARGET_SPEED_1 // unit:RPM, if over this speed, the smo BEMF FIR LPF will be disabled, it is not for PLL

//---add non protect speed error time after enter into close loop--------------//20200506------------------------------------------------------------------------------
#define NON_PROTECT_SPEED_ERROR_TIME_AFTER_CLOSE_LOOP 100 // unit:ms, if 100,it will disable checking speed error for 100ms after enter into close loop

//---add decoupling control----------------------------------------------------//20200522-------------------------------------------------------------------------------
//#define ENABLE_DECOUPLING_CONTROL                               //if enable, it will run d,q axis decoupling control

#define DECOUPLING_CONTROL_MAX_OUTPUT_FOR_Vd_COMPONENT 6000
#define DECOUPLING_CONTROL_MAX_OUTPUT_FOR_Vq_COMPONENT 6000

#define ROTOR_FLUX_PARAMETER_K 4 // unit:0.01  for We * rotor flux

#define DECOUPLING_Iq_Kp_VALUE 2000 // PI controller parameter kp for Iq decoupling control
#define DECOUPLING_Iq_Ki_VALUE 100  // 100  //PI controller parameter ki for Iq decoupling control

#define DECOUPLING_Id_Kp_VALUE 2000 // PI controller parameter kp for Id decoupling control
#define DECOUPLING_Id_Ki_VALUE 100  // 100  //PI controller parameter ki for Id decoupling control

//----add rotation inverse detection control--------------------------------------//20200611-----------------------------------------------------------------------------
//----it must disable this enable define if the motor is square wave Magnetizing
//#define ENABLE_ROTATION_INVERSE_PROTECTION

//********************************************************************************************************
//** Parameters Auto Calculation
//********************************************************************************************************
#define PWM_PERIOD (SYSYTEM_CLOCK / 2 / PWM_FREQUENCY)                            // 48MHz/(PWM_FREQUENCY*2)
#define CURRENT_GAIN ((SHUNT_R_VALUE * CURRENT_AMPLIFICATION_FACTOR * 655) / 500) // 50*50*655/500 = 3275
#define OPEN_LOOP_RAMP_UP_TARGET_Iq ((OPEN_LOOP_TARGET_CURRENT * CURRENT_GAIN) / 10)
#define OPEN_LOOP_RAMP_DOWM_TARGET_Iq ((OPEN_LOOP_RAMP_DOWN_CURRENT * CURRENT_GAIN) / 10)
#define CURRENT_LIMIT_Iq ((CURRENT_LIMIT * CURRENT_GAIN) / 10)
#define NEW_STARTUP_Iq_SLOP ((NEW_STARTUP_RAMP_UP_CURRENT_SLOP * CURRENT_GAIN) / (10 * 10)) // Iq cmd acceleartion slop in new startup mode//20190626
#define LACK_PHASE_CURRENT_THRESHOLD ((LACK_PHASE_CURRENT * CURRENT_GAIN) / 1000)           // 20190626

#define INJECT_VOLTAGE_PULSE_Vd (INJECT_VOLT_PULSE_AMPLITUDE * 327) // for IPD2

#define HFJ_ANGLE_ADD_PER_PWM_CYCLE ((1024 * HFJ_FREQUENCY) / PWM_FREQUENCY) // For IPD3 HFJ rotot position detection

#define LOW_SIDE_MIN_PWM_DUTY (PWM_PERIOD - ((PWM_PERIOD * MAX_PWM_DUTY_PERCENTAGE_2SHUNT_R) / 100))

#define TARGET_Iq_COMMAND_1 (-(TARGET_TORQUE_CURRENT_1 * CURRENT_GAIN) / 10) // 20191122
#define TARGET_Iq_COMMAND_2 (-(TARGET_TORQUE_CURRENT_2 * CURRENT_GAIN) / 10) // 20191122
#define TARGET_Iq_COMMAND_3 (-(TARGET_TORQUE_CURRENT_3 * CURRENT_GAIN) / 10) // 20191122
#define TARGET_Iq_COMMAND_4 (-(TARGET_TORQUE_CURRENT_4 * CURRENT_GAIN) / 10) // 20191122
#define TARGET_Iq_COMMAND_5 (-(TARGET_TORQUE_CURRENT_5 * CURRENT_GAIN) / 10) // 20191122
#define TARGET_Iq_COMMAND_6 (-(TARGET_TORQUE_CURRENT_6 * CURRENT_GAIN) / 10) // 20191122
#define TARGET_Iq_COMMAND_7 (-(TARGET_TORQUE_CURRENT_7 * CURRENT_GAIN) / 10) // 20191122

#define Iq_CMD_INPUT_Iq_SLOP ((Iq_CMD_INPUT_RAMP_UP_CURRENT_SLOP * CURRENT_GAIN) / (10 * 10)) // Iq cmd acceleartion slop in new startup mode//20190626

#define MTPA_MAX_Id_CMD_LIMIT_ALL ((MTPA_MAX_Id_CURRENT_LIMIT_ALL * CURRENT_GAIN) / 10)     // 20200303
#define CURRENT_LIMIT_Is_MAX ((CURRENT_LIMIT * CURRENT_GAIN) / 9)                           // for field weakening in MTPA constant power area//20200303
#define CURRENT_LIMIT_Is_MIN ((CURRENT_LIMIT * CURRENT_GAIN) / 11)                          // for field weakening in MTPA constant power area//20200303
#define CONST_POWER_Vsref_TARGET ((32767 * CONST_POWER_Vsref_VALUE) / 100)                  // for field weakening in MTPA constant power area//20200303
#define MTPA_PI_FW_MAX_Id_CMD_LIMIT ((MTPA_PI_FW_Id_MAX_CURRENT_LIMIT * CURRENT_GAIN) / 10) // 20200303

#define PWM_CYCLES_FOR_INCREASE_1_ANGLE_CW (NEW_STARTUP_RAMP_UP_TIME / NEW_STARTUP_RAMP_UP_ANGLE_CW)   // 20200325
#define PWM_CYCLES_FOR_INCREASE_1_ANGLE_CCW (NEW_STARTUP_RAMP_UP_TIME / NEW_STARTUP_RAMP_UP_ANGLE_CCW) // 20200325

#define MOTOR_RUN_CW 1
#define MOTOR_RUN_CCW 0

#define SMO_I_STATE_DIV4 4    // DIVIDE 4   // 4/16 //20200727
#define SMO_I_STATE_DIV3_5 5  // DIVIDE 3.5 // 5/16
#define SMO_I_STATE_DIV3 6    // DIVIDE 3   // 6/16
#define SMO_I_STATE_DIV2_5 7  // DIVIDE 2.5 // 7/16
#define SMO_I_STATE_DIV2 8    // DIVIDE 2   // 8/16
#define SMO_I_STATE_DIV1_5 12 // DIVIDE 1.5 // 12/16
#define SMO_I_STATE_MUL1 16   // MUL   16/16 =1
#define SMO_I_STATE_MUL1_5 24 // MUL   24/16 =1.5
#define SMO_I_STATE_MUL2 32   // MUL   32/16 =2
#define SMO_I_STATE_MUL2_5 40 // MUL   40/16 =2.5
#define SMO_I_STATE_MUL3 48   // MUL   48/16 =3
#define SMO_I_STATE_MUL3_5 56 // MUL   56/16 =3.5
#define SMO_I_STATE_MUL4 64   // MUL   64/16 =4

#ifdef ENABLE_COMP1_AND_iVREF_INTER_CONNECT_TIM1BKIN_DO_OVER_CURRENT_PROTECT
//----define over current detection TIM1_BKIN1's output to disable Motor PWM function enable or not--------------------------------------------------------------
#define ENABLE_OVER_CURRENT_TIM1BKIN_PROTECTION // enable this to do over current protection (PWM OFF), if TIM1_BKIN pin receive high signal
//----define over current detection comparator1's output to connect the internal TIM1BKIN------------------------------------------------------------------------
#define ENABLE_OVER_CURRENT_COMP1_PROTECTION // enable this to do over current protection (PWM OFF), if comparator1 output high signal
#endif

#ifdef ENABLE_TIM1BKIN_INPUT_PIN_ONLY_DO_OVER_CURRENT_PROTECT
//----define over current detection TIM1_BKIN1's output to disable Motor PWM function enable or not--------------------------------------------------------------
//#define ENABLE_OVER_CURRENT_TIM1BKIN_PROTECTION // enable this to do over current protection (PWM OFF), if TIM1_BKIN pin receive high signal
//!!!!!!!!!!??????
//----define TIM1BKIN signal input from external IO pin, if disable it, the TIM1BKIN input siganl must from internal comparator----------------------------------
#define ENABLE_TIM1BKIN_PIN_EXTERNAL_INPUT // enable TIM1BKIN input signal(MCU PB12 pin)
#endif

//-------run mode parameters define------------------------------------------------------------------------------------------------------------------------------
#define STOP_MODE 0
#define ZERO_CURRENT_CALIBRATE_MODE 1
#define CHECK_BEMF_MODE 2
#define STANDSTILL_BEMF_CALIBRATE_MODE 3
#define DETECT_ROTOR_POSITION_MODE 4
#define RUN_IN_OPEN_LOOP_MODE 5
#define RUN_IN_CLOSE_LOOP_MODE 6
#define ROTOR_SMALL_SWING_MODE 7
#define INDUCTANCE_SAT_POSITION_DET_MODE 8
#define RUN_IN_ALIGNMENT_MODE 9
#define RUN_IN_NEW_STARTUP_MODE 10
#define HFJ_POSITION_DET_MODE 11
#define HFJ_POLARITY_CHECK_MODE 12
#define LOWER_3_ARM_UL_VL_WL_ON_BRAKE_MODE 14

/********************************************************************************************************
** Hardware setup
********************************************************************************************************/
#ifdef ENABLE_SPIN2X
//----define PWM UL pin I/O ploarity -------------------------------------------------------------------------------
#define UL_PIN_NUM GPIO_Pin_6
#define UL_PIN_GROUP GPIOB
#define UL_PIN_SOURCE_NUM GPIO_PinSource6
#define UL_PIN_AF_NUM GPIO_AF_7

#define UL_PIN_IO_MODE_OUTPUT_HIGH UL_PIN_GROUP->BSRR |= UL_PIN_NUM
#define UL_PIN_IO_MODE_OUTPUT_LOW UL_PIN_GROUP->BRR |= UL_PIN_NUM

//----define PWM VL pin I/O ploarity -------------------------------------------------------------------------------
#define VL_PIN_NUM GPIO_Pin_4
#define VL_PIN_GROUP GPIOB
#define VL_PIN_SOURCE_NUM GPIO_PinSource4
#define VL_PIN_AF_NUM GPIO_AF_7

#define VL_PIN_IO_MODE_OUTPUT_HIGH VL_PIN_GROUP->BSRR |= VL_PIN_NUM
#define VL_PIN_IO_MODE_OUTPUT_LOW VL_PIN_GROUP->BRR |= VL_PIN_NUM
//----define PWM UH pin I/O ploarity -------------------------------------------------------------------------------
#define UH_PIN_NUM GPIO_Pin_7
#define UH_PIN_GROUP GPIOB
#define UH_PIN_SOURCE_NUM GPIO_PinSource7
#define UH_PIN_AF_NUM GPIO_AF_7

#define UH_PIN_IO_MODE_OUTPUT_HIGH UH_PIN_GROUP->BSRR |= UH_PIN_NUM
#define UH_PIN_IO_MODE_OUTPUT_LOW UH_PIN_GROUP->BRR |= UH_PIN_NUM
//----define PWM VH pin I/O ploarity -------------------------------------------------------------------------------
#define VH_PIN_NUM GPIO_Pin_5
#define VH_PIN_GROUP GPIOB
#define VH_PIN_SOURCE_NUM GPIO_PinSource5
#define VH_PIN_AF_NUM GPIO_AF_7

#define VH_PIN_IO_MODE_OUTPUT_HIGH VH_PIN_GROUP->BSRR |= VH_PIN_NUM
#define VH_PIN_IO_MODE_OUTPUT_LOW VH_PIN_GROUP->BRR |= VH_PIN_NUM
//----define PWM WH pin I/O ploarity in IPD mode-------------------------------------------------------------------------------
#define WH_PIN_NUM GPIO_Pin_3
#define WH_PIN_GROUP GPIOB
#define WH_PIN_SOURCE_NUM GPIO_PinSource3
#define WH_PIN_AF_NUM GPIO_AF_7

#define WH_PIN_IO_MODE_OUTPUT_HIGH WH_PIN_GROUP->BSRR |= WH_PIN_NUM
#define WH_PIN_IO_MODE_OUTPUT_LOW WH_PIN_GROUP->BRR |= WH_PIN_NUM

//----define PWM WL pin I/O ploarity in IPD mode-------------------------------------------------------------------------------
#define WL_PIN_NUM GPIO_Pin_15
#define WL_PIN_GROUP GPIOA
#define WL_PIN_SOURCE_NUM GPIO_PinSource15
#define WL_PIN_AF_NUM GPIO_AF_7

#define WL_PIN_IO_MODE_OUTPUT_HIGH WL_PIN_GROUP->BSRR |= WL_PIN_NUM
#define WL_PIN_IO_MODE_OUTPUT_LOW WL_PIN_GROUP->BRR |= WL_PIN_NUM
#endif

//#define UL_PIN_IO_MODE_LOW_ACTIVE     //if define UL pin will active low in IO mode. if not define, UL pin will active high in IO mode
//#define VL_PIN_IO_MODE_LOW_ACTIVE     //if define VL pin will active low in IO mode. if not define, VL pin will active high in IO mode
//#define WL_PIN_IO_MODE_LOW_ACTIVE     //if define WL pin will active low in IO mode. if not define, WL pin will active high in IO mode

//----SPIN2X ADC Hardware channel Define------------------------------------------------------------------------------------------
#ifdef ENABLE_SPIN2X

#ifdef ENABLE_1_SHUNT_R_TO_MEASURE_3_PHASE_CURRENT
//#define ENABLE_OP1_PA4_PA5_PA6    //PA6  output has connect to SPIN2X internal ADC pin
//#define ENABLE_OP2_PB0_PB1_PB2    //PB2  output has connect to SPIN2X internal ADC pin
//#define ENABLE_OP3_PB12_PB11_PB10 //PB10 output has connect to SPIN2X internal ADC pin
//#define ENABLE_OP4_PB15_PA8_PA9 // PA9 output has not connect to SPIN2X internal ADC pin.Need setup PA6(1shuntR current sensing, PA3(ISum)to ADC input pin
#endif

#ifndef ENABLE_1_SHUNT_R_TO_MEASURE_3_PHASE_CURRENT
//#define ENABLE_OP1_PA4_PA5_PA6    // PA6  output has connect to SPIN2X internal ADC pin, Phase current IU
//#define ENABLE_OP2_PB0_PB1_PB2    // PB2  output has connect to SPIN2X internal ADC pin, Phase current IV
//#define ENABLE_OP3_PB12_PB11_PB10 // PB10 output has connect to SPIN2X internal ADC pin, Phase current IW
//#define ENABLE_OP4_PB15_PA8_PA9   //PA9  output has not connect to SPIN2X internal ADC pin//total current ISum(PA3) and 1 Shunt R Current sensing(PA6)
#endif

#define BEMFA_ADC_DATA_REGISTER (ADC1->ADDR0)
#define BEMFB_ADC_DATA_REGISTER (ADC1->ADDR1)
#define PHASEA_CURR_ADC_DATA_REGISTER (ADC1->ADDR6)
#define PHASEB_CURR_ADC_DATA_REGISTER (ADC2->ADDR10)

#define VSP_ADC_DATA_REGISTER (ADC2->ADDR0) // ADC2 Channel 0

#define ADC1_CHANNEL_1_SHUNT_R_REGISTER (ADC1->ADDR11) // ADC1 Channel 6,define the 1 shunt R ADC channel register
#define ADC2_CHANNEL_1_SHUNT_R_REGISTER (ADC2->ADDR11) // ADC2 Channel 6,define the 1 shunt R ADC channel register
#define ISUM_ADC_DATA_REGISTER (ADC1->ADDR3)           // define Isum ADC channel register, for total current of motor
#define VBUS_ADC_DATA_REGISTER (ADC1->ADDR7)           // DC Bus voltage input//20181215

#define ADC_BEMF_A_CHANNEL_ENBALE CHEN0_ENABLE
#define ADC_BEMF_B_CHANNEL_ENBALE CHEN1_ENABLE
#define ADC_BEMF_C_CHANNEL_ENBALE CHEN2_ENABLE
#define ADC_1_SHUNT_R_CHANNEL_ENABLE CHEN11_ENABLE   // ADC1,2 Channel 6
#define ADC_2_SHUNT_R_CHANNEL_U_ENABLE CHEN6_ENABLE  // ADC1 channel 6
#define ADC_2_SHUNT_R_CHANNEL_V_ENABLE CHEN10_ENABLE // ADC2 channel 10

#define ADC_SPEED_CMD_IN_CHANNEL_ENABLE CHEN0_ENABLE // ADC2 Channel 3
#define ADC_ISUM_CHANNEL_ENABLE CHEN3_ENABLE         // ADC1 Channel 3

#define ADC_VBUS_CHANNEL_ENABLE CHEN7_ENABLE // DC Bus voltage input//20181215

#define BEMFA_ADC_CHANNEL 0 // ADC1
#define BEMFB_ADC_CHANNEL 1 // ADC1
#define Ia_ADC_CHANNEL 6    // ADC1
#define Ib_ADC_CHANNEL 6    // ADC2
#define ISUM_ADC_CHANNEL 3  // ADC1

#define LED0_ON GPIOD->BRR |= GPIO_Pin_3   // Green light ON
#define LED0_OFF GPIOD->BSRR |= GPIO_Pin_3 // Green light OFF
#define LED1_ON GPIOD->BRR |= GPIO_Pin_3   // Red light ON
#define LED1_OFF GPIOD->BSRR |= GPIO_Pin_3 // Red light OFF
#define KEY0 GPIO_ReadInputDataBit(GPIOD, GPIO_Pin_2)
#define LED2_ON GPIOD->BRR |= GPIO_Pin_7   // Red light ON
#define LED2_OFF GPIOD->BSRR |= GPIO_Pin_7 // Red light OFF
#define SD_ALL_ON GPIOD->BSRR |= GPIO_Pin_6;
#define SD_ALL_OFF GPIOD->BRR |= GPIO_Pin_6;
//("SD_off"); // SD_port_OFF
#define LED1_TOGGLE() (GPIO_ReadOutputDataBit(GPIOD, GPIO_Pin_3)) ? (GPIO_ResetBits(GPIOD, GPIO_Pin_3)) : (GPIO_SetBits(GPIOD, GPIO_Pin_3))
#define LED_TOGGLE() (GPIO_ReadOutputDataBit(GPIOC, GPIO_Pin_14)) ? (GPIO_ResetBits(GPIOC, GPIO_Pin_14)) : (GPIO_SetBits(GPIOC, GPIO_Pin_14))
#define LED2_TOGGLE() (GPIO_ReadOutputDataBit(GPIOD, GPIO_Pin_7)) ? (GPIO_ResetBits(GPIOD, GPIO_Pin_7)) : (GPIO_SetBits(GPIOD, GPIO_Pin_7))
#define LED3_TOGGLE() (GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_4)) ? (GPIO_ResetBits(GPIOA, GPIO_Pin_4)) : (GPIO_SetBits(GPIOA, GPIO_Pin_4))
#endif

#define ADCDELAY (ADCR_TRGSHIFT_0) // if define ADCR_TRGSHIFT_16, it means ADC will delay 16 period clock then start to convert

#endif
